package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lenovo on 2017/10/31.
 */
public class InsuranceInfoTransfer extends AbstractTransfer{
    public InsuranceInfoTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String insurNo = (String) params.get("insurNo");
        String password = (String) params.get("password");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("INSUR_NO", insurNo).addAttribute("INSUR_PWD", password);
        return tplDoc.asXML();
    }
    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            InsuranceInfoTransfer.InsuranceInfoInnerMessage dataRowMessage = new InsuranceInfoTransfer.InsuranceInfoInnerMessage(dataRow);
            resMap.put("idNo", dataRowMessage.getIdNo());
            resMap.put("name", dataRowMessage.getName());
            resMap.put("sex", dataRowMessage.getSex());
            resMap.put("birthday", dataRowMessage.getBirthday());
            resMap.put("age", dataRowMessage.getAge());
            resMap.put("personalType", dataRowMessage.getPersonalType());
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);


        }
        return resMap;
    }

    private class InsuranceInfoInnerMessage implements InnerMessage {
        /**
         * 身份证号
         */
        private String idNo;
        /**
         * 姓名
         */
        private String name;
        /**
         * 性别
         */
        private String sex;
        /**
         * 生日
         */
        private String birthday;
        /**
         * 年龄
         */
        private String age;
        /**
         * 人员类别
         */
        private String personalType;


        public InsuranceInfoInnerMessage(Element element){
            this.idNo = element.attributeValue("ID_NO");
            this.name = element.attributeValue("NAME");
            this.sex = element.attributeValue("SEX");
            this.birthday = element.attributeValue("BIRTHDAY");
            this.age = element.attributeValue("AGE");
            this.personalType = element.attributeValue("PERSONAL_TYPE");
        }
        public String getIdNo() { return idNo; }

        public String getName() { return name; }

        public String getSex() { return sex; }

        public String getBirthday() { return birthday; }

        public String getAge() { return age; }

        public String getPersonalType() { return personalType; }

    }
}
